package jmine.tec.rpc.client.comm;

import jmine.tec.proxy.builder.ProxyBuilder;
import jmine.tec.proxy.chain.ChainElement;
import jmine.tec.rpc.client.provider.ClientStubFactory;
import jmine.tec.rpc.common.metadata.RestServiceMetadata;
import jmine.tec.rpc.common.serialization.Serializer;
import jmine.tec.utils.io.http.Connector;

/**
 * Factory de client stubs que utiliza o pacote jmine-tec-aop
 * 
 * @author lundberg
 */
public class AOPClientStubFactory implements ClientStubFactory {

    private Connector connector;

    private Serializer serializer;

    private String basePath;

    /**
     * {@inheritDoc}
     */
    public <S> S create(Class<S> service, String prefix) {
        RestServiceMetadata.verifyService(service);
        ChainElement chain = new ClientStubChainElement(this.connector, this.serializer, this.pathFor(service, prefix));
        return ProxyBuilder.proxyFor(null, service, chain);
    }

    /**
     * Finds the correct path for the service
     * 
     * @param service service
     * @param prefix prefix
     * @return string
     */
    private String pathFor(Class<?> service, String prefix) {
        return this.basePath + prefix;
    }

    /**
     * @param connector the connector to set
     */
    public void setConnector(Connector connector) {
        this.connector = connector;
    }

    /**
     * @param serializer the serializer to set
     */
    public void setSerializer(Serializer serializer) {
        this.serializer = serializer;
    }

    /**
     * @param basePath the basePath to set
     */
    public void setBasePath(String basePath) {
        this.basePath = basePath;
    }
}
